package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.JobCategory;
import com.weipai.demo.model.JobNews;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.NewsSaveService;
import com.weipai.demo.service.NewsShowService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/14 10:05
 * @Version 1.0
 */

//兼职信息
@RestController
public class PartTimeJobController {
    private final static Logger logger = LoggerFactory.getLogger(PartTimeJobController.class);

    @Autowired
    private NewsSaveService newsSaveService;
    @Autowired
    private NewsShowService newsShowService;
    @Autowired
    private FileUploadService fileUploadService;

    //兼职信息的添加或修改
    @RequestMapping(value = "/News/save")
    public Response save(JobNews jobNews) {
        return newsSaveService.save(jobNews);
    }


    //兼职信息的删除
    @RequestMapping(value = "/News/deleteById")
    public Response deleteById(JobNews jobNews) {
        return newsSaveService.deleteById(jobNews);
    }

    //==============================================================================================


    //显示消息类列表
    @RequestMapping(value = "/NewsShowList/NewsClassShow")
    public Response NewsClassShow(JobCategory jobCategory){
        return newsShowService.NewsShowList(jobCategory);
    }


    //分页显示消息列表
    @ResponseBody
    @RequestMapping(value = "/NewsShowList/PagingMessage",method = RequestMethod.POST)
    public LayuiTablePagingResult PagingMessage(Integer page,
                                                Integer size,
                                                JobNews jobNews ){
        return  newsShowService.NewsShow(page,size,jobNews);
    }

    //==============================================================================================

    //后台按照标题关键字查询
    @RequestMapping(value = "/NewsShowList/searchByTitle")
    public Response searchByTitle(Integer page,
                                  Integer size,
                                  JobNews jobNews){
        return newsShowService.searchTitle(page,size,jobNews);

    }

    //================================================================================================


    /**
     * 图片的保存
     * @param imgUrl
     * @return
     */
    @PostMapping("/picture/savePicture")
    public Response savePicture(HttpServletRequest request, JobNews jobNews, MultipartFile img){
        Response response = new Response();
        try {
            if(img!=null){
                String result = fileUploadService.saveImgUpload(request, img);
                if (result.equals("formatError")) {
                    response.setCode(RetCode.IMG_FORMAT);
                } else if (result.equals("sizeover")) {
                    response.setCode(RetCode.IMG_SIZE);
                } else if(result.equals("error")){
                    response.setCode(RetCode.IMG_ERROR);
                }else{
                    jobNews.setImgUrl(result);
                    //response = newsShowService.savePicture(jobNews);
                }
            }else{
                //response = newsShowService.savePicture(jobNews);

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        response.put("imgUrl",jobNews.getImgUrl());
        response.setCode(RetCode.SUCCESS);
        return response;
    }

}
